package model;

import model.exception.UnsupportedFileTypeException;

import java.io.File;

/**
 * @author Zach Michaelov
 */
public class FileParserFactory {

    public static FileParser fromFile(File dataFile) {
        String extension = findExtension(dataFile.getName());

        if (extension.equals("csv")) {
            return new CSVFileParser(dataFile);
        }else if (extension.equals("tsv")) {
            return new TSVFileParser(dataFile);
        } else {
            throw new UnsupportedFileTypeException("File type not currently supported", dataFile.getName());
        }

    }

    private static String findExtension (String fileName) {
        int extensionIndex = fileName.lastIndexOf(".");
        if (extensionIndex == -1) {
            throw new UnsupportedFileTypeException(
                    "File contains no extension", fileName);
        }
        return fileName.substring(extensionIndex + 1);
    }
}
